package Components.actionimage
{
	import Components.config.ImageConfig;
	import Components.image.ActionFileName;
	
	import core.RefreshManager;
	
	import events.RefreshEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.Dictionary;

	public class ActionImageController
	{
		private static var _dict:Dictionary = new Dictionary();
		
		private var _container:Sprite;
		private var _c:Sprite;
		private var _bitmap:Bitmap;
		
		private var _content:MovieClip;
		private var _currentFrame:int = 0;
		private var _maxFrame:int = 17;
		private var _name:String = "stand_d";
		private var _direct:int = 0;
		
		private var _application:ApplicationDomain;
		
		private var _parent:DisplayObjectContainer;
		
		public function ActionImageController( parent:DisplayObjectContainer )
		{
			_parent = parent;
			init();
		}
		
		public static function clear():void
		{
			for( var i:* in _dict )
			{
				var ac:ActionImageController = _dict[i];
				delete _dict[i];
				ac.destroy();
			}
		}
		
		public function destroy():void
		{
			if( _c.parent != null )
			{
				_c.parent.removeChild( _c );
			}
			if( _isAdd )
			{
				_isAdd = false;
				RefreshManager.instance.removeEventListener( RefreshEvent.REFRESH, onRefresh );
			}
		}
		
		private function init():void
		{
			_dict[this] = this;
			
			_c = new Sprite();
			_parent.addChildAt( _c, 0 );
			
			_bitmap = new Bitmap();
			_c.addChild( _bitmap );
			
			_application = new ApplicationDomain( ApplicationDomain.currentDomain );
		}
		
		public static function load( url:String ):void
		{
			var ac:ActionImageController = new ActionImageController( ActionEditor.instance.container );
			ac.load( url );
		}
		
		public static function change( action:String, dir:String ):void
		{
			action = ActionFileName.getOutActionName(action) + "_d";
			var d:int = int(ActionFileName.getOutDirectionName(dir).charAt(1));
			for( var i:* in _dict )
			{
				i.change( action, d );
			}
		}
		
		public static function nextFrame():void
		{
			for( var i:* in _dict )
			{
				i.nextFrame();
			}
		}
		
		public function nextFrame():void
		{
			_currentFrame++;
			if( _currentFrame >= _maxFrame )
			{
				_currentFrame = 0;
			}
			refresh();
		}
		
		public function change( action:String, dir:int ):void
		{
			if( _content.hasOwnProperty(action.substr(0,action.length-1)+"l") )
			{
				_name = action;
				_direct = dir;
				_maxFrame = _content[_name.substr(0,_name.length-1)+"l"];
				_currentFrame = 0;
				refresh();
			}
		}
		
		public function load( url:String ):void
		{
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onLoaded );
			loader.load( new URLRequest( url ), new LoaderContext(false, _application) );
		}
		
		private var _isAdd:Boolean = false;
		private function onLoaded( e:Event ):void
		{
			_content = e.currentTarget.content as MovieClip;
			if( _content.hasOwnProperty("stand_l") )
			{
				_isAdd = true;
				_maxFrame = _content["stand_l"];
				refresh();
				RefreshManager.instance.addEventListener( RefreshEvent.REFRESH, onRefresh );
			}
		}
		
		private var _duration:int = 0;
		private function onRefresh( e:RefreshEvent ):void
		{
			_duration += e.data;
			if( _duration >= ImageConfig.duration )
			{
				_duration -= ImageConfig.duration;
				
				refresh();
				_currentFrame++;
				if( _currentFrame >= _maxFrame )
				{
					_currentFrame = 0;
				}
			}
		}
		
		private function refresh():void
		{
			var dir:int = _direct;
			if( _direct==5 ) dir = 3;
			else if( _direct==6 ) dir = 2;
			else if( _direct==7 ) dir = 1;
			
			var name:String = _name + dir + "_" + _currentFrame;
			if( _content.loaderInfo.applicationDomain.hasDefinition(name) )
			{
				var cl:Class = _content.loaderInfo.applicationDomain.getDefinition(name) as Class;
				var bd:BitmapData = new cl(0,0) as BitmapData;
				
				_bitmap.bitmapData = bd;
				var x:int = _content[name+"_x"];
				var y:int = _content[name+"_y"];
				if( _direct != dir )
				{
					x = -x;
					_bitmap.scaleX = -1;
				}
				
				_bitmap.x = x;
				_bitmap.y = y;
			}
			else _bitmap.bitmapData = null;
		}
	}
}